package org.springblade.modules.repair.utils;

import cn.hutool.core.date.DateUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/9/8 8:34
 */
public class DateUtils {

	public final static Integer HOURS_MINUTE = 60;

	/**
	 * 分钟转为小时
	 *
	 * @param minuteStr 分钟数
	 * @return 数据集
	 */
	public static String minuteToHours(int minuteStr) {
		String resultStr = "";
		if (minuteStr != 0) {
			if (minuteStr / 60 == 0) {
				resultStr = "0时" + minuteStr % 60 + "分";
			} else {
				if (minuteStr % 60 == 0) {
					resultStr = minuteStr / 60 + "小时";
				} else {
					resultStr = (minuteStr / 60 + "小时" + minuteStr % 60 + "分");
				}
			}
		} else {
			resultStr = "0时0分";
		}
		return resultStr;
	}

	/**
	 * 格式：yyyy-m-dd (今天/明天/后天)
	 * @param date 时间
	 * @return 时间描述结果
	 */
	public static String chineseDate(Date date) {
		StringBuilder str = new StringBuilder(DateUtil.format(date, "yyyy-M-dd"));
		long day = DateUtil.betweenDay(DateUtil.date(), date, true);
		if (day == 0) {
			str.append(" (今天) ");
		} else if (day == 1) {
			str.append(" (明天) ");
		} else if (day == 2) {
			str.append(" (后天) ");
		}
		return str.toString() + DateUtil.format(date,"HH:mm");
	}

	/**
	 * 2025-06 适配UI格式，时间缩短到月份和日期
	 * @param date 时间
	 * @return 时间描述结果
	 */
	public static String newChineseDate(Date date){
		StringBuilder str = new StringBuilder(DateUtil.format(date, "M月d日"));
		long day = DateUtil.betweenDay(DateUtil.date(), date, true);
		if (day == 0) {
			str.append("(今天)");
		} else if (day == 1) {
			str.append("(明天)");
		} else if (day == 2) {
			str.append("(后天)");
		}
		String result = str.toString();
		return result + DateUtil.format(date,"HH:mm");
	}




	public static Date beijingDate(){
		LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC+8"));
		long time = Date.from(now.atZone(ZoneId.systemDefault()).toInstant()).getTime();
		return new Date(time);
	}


	/**
	 * 获取当前数据的结束时间(秒级别)
	 * 比如：2024-11-26 08:45:12
	 * 返回 2024-11-26 08:46:00
	 *
	 * @param date 时间
	 * @return 数据集
	 */
	public static Long getNowDateEnd(Date date) {
		return DateUtil.parse(DateUtil.format(new Date(date.getTime() + 60000), "yyyy-MM-dd HH:mm") + " 00").getTime();
	}

	public static class TimePeriod {
		Date start;
		Date end;

		public TimePeriod(Date start, Date end) {
			this.start = start;
			this.end = end;
		}
	}

	/**
	 * 计算出所有时间段的总时长
	 * @param periods
	 * @return
	 */
	public static long calculateWorkDuration(List<TimePeriod> periods) {
		if (periods == null || periods.isEmpty()) {
			return 0;
		}

		// 按开始时间对时段进行排序
		Collections.sort(periods, Comparator.comparing(p -> p.start));

		List<TimePeriod> mergedPeriods = new ArrayList<>();
		TimePeriod currentPeriod = periods.get(0);

		for (int i = 1; i < periods.size(); i++) {
			TimePeriod nextPeriod = periods.get(i);
			if (nextPeriod.start.before(currentPeriod.end) || nextPeriod.start.equals(currentPeriod.end)) {
				// 合并重叠时间段
				currentPeriod.end = nextPeriod.end.after(currentPeriod.end) ? nextPeriod.end : currentPeriod.end;
			} else {
				// 非重叠时间段，将当前添加到合并列表并移至下一个
				mergedPeriods.add(currentPeriod);
				currentPeriod = nextPeriod;
			}
		}
		mergedPeriods.add(currentPeriod);

		// 计算总持续时间
		long totalDuration = 0;
		for (TimePeriod period : mergedPeriods) {
			totalDuration += (period.end.getTime() - period.start.getTime()); // Convert to minutes
		}

		// 转分钟向上取整
		return (long) Math.ceil(totalDuration / 60000.0);
	}

	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(chineseDate(date));
		System.out.println(newChineseDate(date));
	}
}
